package cn.gqr.controller.adm;

import cn.gqr.entity.Hot;
import cn.gqr.entity.Largefile;
import cn.gqr.service.HotService;
import cn.gqr.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

//后台热点api
@Controller
@RequestMapping("/api/adm/hot")
public class HotController {

    @Autowired
    HotService hotService;

    //添加
    @ResponseBody
    @RequestMapping("/addHot")
    public Map<String,Object> addHot(Hot hot, @RequestParam(value = "pic",required = false)
            MultipartFile file,Largefile largefile) {
        //添加hot并且上传图片 涉及两张表的变动，需开启事务保护
        return hotService.addHot(hot,file,largefile);
    }

    //全查询
    @ResponseBody
    @RequestMapping("/findAll")
    public Map<String,Object> findAll(){
        List<Hot> hots = hotService.findAll();
        return RespJson.pack(200,"ok",hots);
    }

    //删除
    @ResponseBody
    @RequestMapping("/delete/{id}")
    public Map<String, Object> delete(@PathVariable("id") int id){
        int i = hotService.delete(id);
        if(i==0){
            return RespJson.pack(204,"删除失败...",null);
        }
        return RespJson.pack(200,"删除成功...",hotService.findAll());
    }

}